package com.cirnoworks.uhs.client;

import java.io.IOException;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.Selector;

import com.cirnoworks.uhs.Link;
import com.cirnoworks.uhs.LinkManager;

public class ClientSubLink extends Link {
	long lastUpdateTime;
	SocketAddress address;
	SocketAddress target;
	Link parent;

	public ClientSubLink(Link parent, Selector selector, LinkManager manager,
			SocketAddress address, SocketAddress target) throws IOException {
		super(selector, manager);
		this.address = address;
		channel.connect(address);
		this.parent = parent;
		this.target = target;
	}

	public void sendData(Selector selector, ByteBuffer data) throws IOException {
		lastUpdateTime = System.currentTimeMillis();
		sendData(selector, address, data);
	}

	@Override
	protected void receiveData(Selector selector, SocketAddress from,
			ByteBuffer data) throws IOException {
		lastUpdateTime = System.currentTimeMillis();
		parent.sendData(selector, target, data);
	}

	public boolean isTimeout() {
		return System.currentTimeMillis() - lastUpdateTime > 300000;
	}

	@Override
	public void onClose() {

	}
}
